package edu.ccut.titan.tag.report.chart.dataset;

import net.sf.jasperreports.charts.design.JRDesignPieDataset;
import net.sf.jasperreports.engine.JRChartDataset;
import net.sf.jasperreports.engine.design.JRDesignExpression;
import edu.ccut.titan.tag.report.chart.IType;
import edu.ccut.titan.tag.report.design.ChartsInfo;
import edu.ccut.titan.tag.report.design.SeriesInfo;

public class TitanPieDataset implements ITitanDataset {

	public JRChartDataset getDatasetContent(ChartsInfo chart) {
		JRDesignPieDataset designPieDataset = new JRDesignPieDataset(null);

		JRDesignExpression keyExpression = new JRDesignExpression();
		SeriesInfo series = chart.getSeries().get(0);
		if (IType.FIELD_TYPE_FIELD.equals(series.getKeytype())
				|| series.getKeytype() == null) {
			keyExpression.addFieldChunk(series.getKey());
		} else if (IType.FIELD_TYPE_PARAMETER.equals(series.getKeytype())) {
			keyExpression.addParameterChunk(series.getKey());
		} else if (IType.FIELD_TYPE_RESOURCE.equals(series.getKeytype())) {
			keyExpression.addResourceChunk(series.getKey());
		} else if (IType.FIELD_TYPE_VARIABLE.equals(series.getKeytype())) {
			keyExpression.addVariableChunk(series.getKey());
		}
		keyExpression.setValueClassName("java.lang.Object");
		designPieDataset.setKeyExpression(keyExpression);

		JRDesignExpression valueExpression = new JRDesignExpression();
		if (IType.FIELD_TYPE_FIELD.equals(series.getValuetype())
				|| series.getValuetype() == null) {
			valueExpression.addFieldChunk(series.getValue());
		} else if (IType.FIELD_TYPE_PARAMETER.equals(series.getValuetype())) {
			valueExpression.addParameterChunk(series.getValue());
		} else if (IType.FIELD_TYPE_RESOURCE.equals(series.getValuetype())) {
			valueExpression.addResourceChunk(series.getValue());
		} else if (IType.FIELD_TYPE_VARIABLE.equals(series.getValuetype())) {
			valueExpression.addVariableChunk(series.getValue());
		}
		valueExpression.setValueClassName("java.lang.Object");
		designPieDataset.setValueExpression(valueExpression);

		return designPieDataset;
	}
}
